/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyThread;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import javax.swing.JTable;

/**
 *
 * @author QuangHau
 */
public class listenRequest extends Thread {

    private JTable tbChunk;

    public listenRequest(JTable _tbChunk) {
        tbChunk = _tbChunk;
    }
    
    public void run() {
        try {

            int port = 35617;  //port socket used to listen
            //khoi tao socket
            DatagramSocket socket = new DatagramSocket(port);
            do {
                //Tao DatagramPacket de nhan data
                byte[] rcvbuf = new byte[1024];
                DatagramPacket rcvpkt = new DatagramPacket(rcvbuf, rcvbuf.length);

                //Goi ham nhan DatagramPacket
                socket.receive(rcvpkt);
                //Comvar.listPacket.add(rcvpkt);
                //Comvar.threadSwitchRequest.release();

                switch (rcvpkt.getData()[0]) {
                    case 10://Find chunk
                        responFindChunk rpcThread = new responFindChunk(rcvpkt);
                        rpcThread.start();
                        break;
                    case 11://respon Find chunk
                        UpdateChunk udcThread = new UpdateChunk(rcvpkt, tbChunk);
                        udcThread.start();
                        break;
                    case 100://request downd chunk
                        UploadChunk uploadThread = new UploadChunk(rcvpkt);
                        uploadThread.start();
                        break;
                    //....
                }

            } while (true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
